package com.cdtye.shuoh.util.text;

/**
 * 图片转成十六进制
 */

import org.apache.http.entity.ContentType;
import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.mock.web.MockMultipartFile;

import java.io.*;

/**
 * 图片转成十六进制
 * a55a060031314d30303030303050563030303030310ac100ff000053e363fa9796
 *
 *
 * A55A040031314D3232313132325056474C3030303205B4BB00FF00A3977B96
 *
 * a55a260031314d303030303030505630303030303109c100d452e36333334741000000000000c2420100000c43000000000000000000000000000102040af85696
 * a55a2600574158303030303030313132323030303109c1dd44f13f6400004041000000000000a44201010000000100000000c3170000930b0000010000014d9d96
 */
public class Test
{
    public static void main(String[] args) throws Exception
    {
//        String message = "A5 5A 06 00 31 31 4D 32 32 31 31 32 32 50 56 47 4C 30 30 30 32 05 B6 EB 00 FF 80 CA C1 63 A7 5F 96";
//        message = message.replaceAll("\\s*", "").toUpperCase();
//        System.out.println(message);
//        System.out.println(message.substring(21*2,22*2));
//        System.out.println(message.substring(22*2,23*2));
//        try
//        {
//            StringBuffer sb = new StringBuffer();
//            FileInputStream fis = new FileInputStream("D:/aaa.jpg");
//            BufferedInputStream bis = new BufferedInputStream(fis);
//            java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
//            byte[] buff = new byte[1024];
//            int len = 0;
//            while ((len = fis.read(buff)) != -1)
//            {
//                bos.write(buff, 0, len);
//            }
//            // 得到图片的字节数组
//            byte[] result = bos.toByteArray();
//
//            MockMultipartFile mockMultipartFile = null;
//            //java7新特性  不用手动关闭流
//            try (InputStream inputStream = new ByteArrayInputStream(result)) {
//                mockMultipartFile = new MockMultipartFile("copy"+"222.jpg","222.jpg","application/octet-stream", inputStream);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            File fileImage = new File("D:/",mockMultipartFile.getOriginalFilename());
//            System.out.println(fileImage.getName());
//            mockMultipartFile.transferTo(fileImage);
//            System.out.println("++++" + byte2HexStr(result));
//            // 字节数组转成十六进制
//            String str = byte2HexStr(result);
//            /*
//             * 将十六进制串保存到txt文件中
//             */
//            PrintWriter pw = new PrintWriter(
//                    new FileWriter("D:/aaa.txt"));
//            pw.println(str);
//            pw.close();
//        }
//        catch (IOException e)
//        {
//            e.printStackTrace();
//        }
        //System.out.println("A55A040031314D3232313132325056474C3030303205B4BB00FF00A3977B96".toLowerCase());
//        String string = "A55A060031314D30303030303050563030303030310AC100FF009E873F64028E196";
//        byte[] bytes = DataConverter.hexString2Bytes(string.toLowerCase());
//        String hexStr = DataConverter.parseByte2HexStr(bytes);
//        System.out.println(hexStr);
       // System.out.println(DataConverter.bytes2HexString(DataConverter.hexString2Bytes(string.toLowerCase())));

        System.out.println(DataConverter.reverseHex(DataConverter.parseInteger2Hex(6,4)));
        System.out.println(System.getProperty("java.library.path"));
    }

    /*
     * 实现字节数组向十六进制的转换方法一
     */
    public static String byte2HexStr(byte[] b)
    {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++)
        {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }

    private static byte uniteBytes(String src0, String src1)
    {
        byte b0 = Byte.decode("0x" + src0).byteValue();
        b0 = (byte) (b0 << 4);
        byte b1 = Byte.decode("0x" + src1).byteValue();
        byte ret = (byte) (b0 | b1);
        return ret;
    }

    /*
     * 实现字节数组向十六进制的转换的方法二
     */
    public static String bytesToHexString(byte[] src)
    {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0)
        {
            return null;
        }
        for (int i = 0; i < src.length; i++)
        {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2)
            {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
}